<!--
 * @Description: 投入类型管理页面
-->
<template>
  <BaseMainSlot :show-search-box="false">
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        :has-save="false"
        :has-cancel="false"
        @save="save('formData')"
        @add="add"
        @cancel="cancel('formData')"
        @edit="edit"
        @del="del"
      />
    </template>
    <template v-slot:table-box>
      <el-tree
        ref="tree"
        :props="treerule"
        :data="treeData"
        show-checkbox
        node-key="inputTypeId"
        default-expand-all
        highlight-current
        check-strictly
        @node-click="handleEdit"
        @check-change="handleSelectionChange"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog :title="dialogTitle + '投入名称信息'" :visible.sync="dialogFormVisible" :before-close="cancel">
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          class="el-form"
        >
          <el-form-item label="类型名称:" prop="typeName" class="w100p">
            <el-input
              v-model="formData.typeName"
              placeholder="请输入类型名称"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注:" prop="specification" class="w100p">
            <el-input
              v-model="formData.remark"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              clearable
            />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button class="zh-button-two" type="primary" @click="save('formData')">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import basicInfoManage from "@/api/basic-info-manage/input-type-manage/index";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import { mymixin } from "@/utils/mixins";
export default {
  name: "",
  components: { ActionBar, BaseMainSlot },
  mixins: [mymixin],
  data() {
    return {
      treerule: {
        children: "child",
        label: "typeName"
      },
      rules: {
        typeName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }]
      },
      treeData: [],
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  created() {
  },
  mounted() {
    // 获取树状图
    this.getTreeDataList();
  },
  methods: {
    getTreeDataList() {
      this.loading = true;
      this.formData = {};
      basicInfoManage.getTreeList().then(res => {
        this.loading = false;
        let data = res.data;
        // 赋值数组
        if (data) {
          this.treeData = data;
          // console.log(this.treeData);
          this.resetActionBar();
        }
      });
    },
    add() {
      this.canInput = true;
      this.formData = {};
      this.openDialog('add');
    },
    handleEdit(row) {
      // TODO 点击行元素后 edit
      this.formData = JSON.parse(JSON.stringify(row));
      this.editActive = true;
      this.saveActive = false;
      this.cancelActive = true;
      this.canInput = false;
      this.$refs.formData.resetFields();
      this.openDialog('edit');
    },
    handleSelectionChange() {
      let list = this.$refs.tree.getCheckedKeys();
      this.inputTypeIdList = [];
      this.inputTypeIdList = list;
      if (this.inputTypeIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      basicInfoManage
        .allmetInfo({ inputTypeIdList: this.inputTypeIdList }, "delList")
        .then(res => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.$refs.formData.resetFields();
            this.getTreeDataList();
          } else {
            this.$message.warning(res.msg || '删除失败');
          }
        });
    },
    save(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            // 编辑接口
            let nowdataone = {
              remark: this.formData.remark,
              parentId: this.formData.parentId,
              typeName: this.formData.typeName,
              inputTypeId: this.formData.inputTypeId
            };
            // // 新增点的对象数据数组
            // let arr = this.treeData.filter(
            //   item => item.inputTypeId === this.inputTypeId
            // );
            // let idList = this.treeData.map(item => item.inputTypeId);
            // if (idList.includes(arr[0].inputTypeId)) {
            //   this.$message.error("该巡查点已经存在~");
            // } else {
            basicInfoManage.allmetInfo(nowdataone, "edit").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "修改成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTreeDataList();
              } else {
                this.$message.error(res.msg || "修改失败！");
              }
            });
            // }
          } else if (this.nowChanceStau === "add") {
            let nowdata = {
              organizationNo: sessionStorage.organizationNo,
              remark: this.formData.remark,
              typeName: this.formData.typeName
            };
            if (this.formData.inputTypeId) {
              nowdata.parentId = this.formData.inputTypeId;
            } else {
              nowdata.parentId = 0;
            }
            // 新增接口
            basicInfoManage.allmetInfo(nowdata, "add").then(res => {
              if (res.code === 1) {
                this.$message.success(res.msg || "新增成功！");
                this.closeDialog();
                this.$refs[formData].resetFields();
                this.getTreeDataList();
              } else {
                this.$message.error(res.msg || "新增失败！");
              }
            });
          } else {
            this.$message.fail("选择失败！");
          }
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      this.dialogTitle = type === 'add' ? '新增' : '编辑'
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = '';
    }
  }
};
</script>

<style scoped lang="scss"></style>
